package cn.cheln.support.images;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.HandlerThread;
import android.os.Message;
import android.widget.ImageView;
import cn.cheln.explorer.config.Constants;
import java.io.File;
import java.lang.ref.SoftReference;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class FileIconLoader
  implements Handler.Callback
{
  private static final String LOADER_THREAD_NAME = "FileIconLoader";
  private static final int MESSAGE_ICON_LOADED = 2;
  private static final int MESSAGE_REQUEST_LOADING = 1;
  private static final ConcurrentHashMap<String, ImageHolder> mImageCache = new ConcurrentHashMap();
  private IconLoadFinishListener iconLoadListener;
  private final Context mContext;
  private LoaderThread mLoaderThread;
  private boolean mLoadingRequested;
  private final Handler mMainThreadHandler = new Handler(???);
  private boolean mPaused;
  private final ConcurrentHashMap<ImageView, FileId> mPendingRequests = new ConcurrentHashMap();

  public FileIconLoader(Context paramContext)
  {
    this.mContext = paramContext;
  }

  private boolean loadCachedIcon(ImageView paramImageView, String paramString, FileTypeInfo paramFileTypeInfo)
  {
    ImageHolder localImageHolder = (ImageHolder)mImageCache.get(paramString);
    if (localImageHolder == null)
    {
      localImageHolder = ImageHolder.create(paramFileTypeInfo);
      if (localImageHolder == null)
        return false;
      mImageCache.put(paramString, localImageHolder);
    }
    do
    {
      do
      {
        localImageHolder.state = 0;
        return false;
      }
      while (localImageHolder.state != 2);
      if (localImageHolder.isNull())
        return true;
    }
    while (!(localImageHolder.setImageView(paramImageView)));
    return true;
  }

  private void processLoadedIcons()
  {
    Iterator localIterator = this.mPendingRequests.keySet().iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
      {
        if (!(this.mPendingRequests.isEmpty()))
          requestLoading();
        return;
      }
      ImageView localImageView = (ImageView)localIterator.next();
      FileId localFileId = (FileId)this.mPendingRequests.get(localImageView);
      if (!(loadCachedIcon(localImageView, localFileId.mPath, localFileId.mCategory)))
        continue;
      localIterator.remove();
    }
  }

  private void requestLoading()
  {
    if (this.mLoadingRequested)
      return;
    this.mLoadingRequested = true;
    this.mMainThreadHandler.sendEmptyMessage(1);
  }

  public void cancelRequest(ImageView paramImageView)
  {
    this.mPendingRequests.remove(paramImageView);
  }

  public void clear()
  {
    this.mPendingRequests.clear();
    mImageCache.clear();
  }

  public String getThumbName(String paramString)
  {
    if (paramString.length() > 249)
      paramString = paramString.substring(6 + -255 + paramString.length());
    return Constants.Thumb_folder + "/" + paramString.replaceAll("/", "#") + ".thumb";
  }

  public boolean handleMessage(Message paramMessage)
  {
    int i = 1;
    switch (paramMessage.what)
    {
    default:
      i = 0;
    case 1:
    case 2:
    }
    do
    {
      do
      {
        return i;
        this.mLoadingRequested = false;
      }
      while (this.mPaused);
      if (this.mLoaderThread == null)
      {
        this.mLoaderThread = new LoaderThread();
        this.mLoaderThread.start();
      }
      this.mLoaderThread.requestLoading();
      return i;
    }
    while (this.mPaused);
    processLoadedIcons();
    return i;
  }

  public boolean loadIcon(ImageView paramImageView, String paramString, long paramLong, FileTypeInfo paramFileTypeInfo)
  {
    boolean bool = loadCachedIcon(paramImageView, paramString, paramFileTypeInfo);
    if (bool)
      this.mPendingRequests.remove(paramImageView);
    do
    {
      return bool;
      FileId localFileId = new FileId(paramString, paramLong, paramFileTypeInfo);
      this.mPendingRequests.put(paramImageView, localFileId);
    }
    while (this.mPaused);
    requestLoading();
    return bool;
  }

  public void pause()
  {
    this.mPaused = true;
  }

  public void resume()
  {
    this.mPaused = false;
    if (this.mPendingRequests.isEmpty())
      return;
    requestLoading();
  }

  public void stop()
  {
    pause();
    if (this.mLoaderThread != null)
    {
      this.mLoaderThread.quit();
      this.mLoaderThread = null;
    }
    clear();
  }

  private static class BitmapHolder extends FileIconLoader.ImageHolder
  {
    SoftReference<Bitmap> bitmapRef;

    private BitmapHolder()
    {
      super(null);
    }

    public boolean isNull()
    {
      return (this.bitmapRef == null);
    }

    public void setImage(Object paramObject)
    {
      if (paramObject == null);
      for (SoftReference localSoftReference = null; ; localSoftReference = new SoftReference((Bitmap)paramObject))
      {
        this.bitmapRef = localSoftReference;
        return;
      }
    }

    public boolean setImageView(ImageView paramImageView)
    {
      if (this.bitmapRef.get() == null)
        return false;
      paramImageView.setImageBitmap((Bitmap)this.bitmapRef.get());
      return true;
    }
  }

  private static class DrawableHolder extends FileIconLoader.ImageHolder
  {
    SoftReference<Drawable> drawableRef;

    private DrawableHolder()
    {
      super(null);
    }

    public boolean isNull()
    {
      return (this.drawableRef == null);
    }

    public void setImage(Object paramObject)
    {
      if (paramObject == null);
      for (SoftReference localSoftReference = null; ; localSoftReference = new SoftReference((Drawable)paramObject))
      {
        this.drawableRef = localSoftReference;
        return;
      }
    }

    public boolean setImageView(ImageView paramImageView)
    {
      if (this.drawableRef.get() == null)
        return false;
      paramImageView.setImageDrawable((Drawable)this.drawableRef.get());
      return true;
    }
  }

  public static class FileId
  {
    public FileTypeInfo mCategory;
    public long mId;
    public String mPath;

    public FileId(String paramString, long paramLong, FileTypeInfo paramFileTypeInfo)
    {
      this.mPath = paramString;
      this.mId = paramLong;
      this.mCategory = paramFileTypeInfo;
    }
  }

  public static abstract interface IconLoadFinishListener
  {
    public abstract void onIconLoadFinished(ImageView paramImageView);
  }

  private static abstract class ImageHolder
  {
    public static final int LOADED = 2;
    public static final int LOADING = 1;
    public static final int NEEDED;
    public FileTypeInfo cate;
    int state;

    public static ImageHolder create(FileTypeInfo paramFileTypeInfo)
    {
      switch ($SWITCH_TABLE$cn$cheln$support$images$FileCategoryHelper$FileCategory()[paramFileTypeInfo.fc.ordinal()])
      {
      case 5:
      case 6:
      case 7:
      default:
        return null;
      case 2:
      case 3:
      case 4:
      case 8:
      }
      FileIconLoader.BitmapHolder localBitmapHolder = new FileIconLoader.BitmapHolder(null);
      localBitmapHolder.cate = paramFileTypeInfo;
      return localBitmapHolder;
    }

    public abstract boolean isNull();

    public abstract void setImage(Object paramObject);

    public abstract boolean setImageView(ImageView paramImageView);
  }

  private class LoaderThread extends HandlerThread
    implements Handler.Callback
  {
    private Handler mLoaderThreadHandler;

    public LoaderThread()
    {
      super("FileIconLoader");
    }

    public boolean handleMessage(Message paramMessage)
    {
      label16: FileIconLoader.FileId localFileId;
      FileIconLoader.ImageHolder localImageHolder;
      File localFile2;
      Iterator localIterator = FileIconLoader.this.mPendingRequests.values().iterator();
      do
      {
        if (!(localIterator.hasNext()))
        {
          FileIconLoader.this.mMainThreadHandler.sendEmptyMessage(2);
          return true;
        }
        localFileId = (FileIconLoader.FileId)localIterator.next();
        localImageHolder = (FileIconLoader.ImageHolder)FileIconLoader.mImageCache.get(localFileId.mPath);
      }
      while ((localImageHolder == null) || (localImageHolder.state != 0));
      File localFile1 = new File(Constants.Thumb_folder);
      if (!(localFile1.exists()))
        localFile1.mkdirs();
      localImageHolder.state = 1;
      Object localObject = null;
      try
      {
        localFile2 = new File(FileIconLoader.this.getThumbName(localFileId.mPath));
      }
      catch (Exception localException2)
      {
        try
        {
          Bitmap localBitmap;
          boolean bool = localFile2.exists();
          localObject = null;
          if (!(bool))
          {
            localObject = readPicBitmap(localFileId, null);
            if (localObject != null);
          }
          for (localObject = localImageHolder.cate.defaultBg; ; localObject = localBitmap)
          {
            while (true)
            {
              if (localObject != null)
                localImageHolder.setImage(localObject);
              localImageHolder.state = 2;
              FileIconLoader.mImageCache.put(localFileId.mPath, localImageHolder);
              break label16:
              Utillocal.compressBitmapToFile(FileIconLoader.this.mContext, FileIconLoader.this.getThumbName(localFileId.mPath), (Bitmap)localObject);
            }
            localBitmap = BitmapFactory.decodeFile(localFile2.getAbsolutePath());
          }
          localException1 = localException1;
          label249: localException1.printStackTrace();
        }
        catch (Exception localException2)
        {
          break label249:
        }
      }
    }

    public Bitmap readPicBitmap(FileIconLoader.FileId paramFileId, Bitmap paramBitmap)
    {
      switch ($SWITCH_TABLE$cn$cheln$support$images$FileCategoryHelper$FileCategory()[paramFileId.mCategory.fc.ordinal()])
      {
      case 5:
      case 6:
      case 7:
      default:
        return paramBitmap;
      case 8:
        return Utillocal.getApkIcon(FileIconLoader.this.mContext, paramFileId.mPath);
      case 2:
      case 4:
        return Utillocal.getImage(paramFileId.mPath, null, FileIconLoader.this.mContext);
      case 3:
      }
      return Utillocal.scaleBitmap(ThumbnailUtils.createVideoThumbnail(paramFileId.mPath, 1));
    }

    public void requestLoading()
    {
      if (this.mLoaderThreadHandler == null)
        this.mLoaderThreadHandler = new Handler(getLooper(), this);
      this.mLoaderThreadHandler.sendEmptyMessage(0);
    }
  }
}

/* Location:           H:\360云盘\lewa_explorer_3.0\bin\FileApp_orig_dex2jar.jar
 * Qualified Name:     cn.cheln.support.images.FileIconLoader
 * JD-Core Version:    0.5.2
 */